<!--  -->
<template>
    <div class="tree-bar">
      <el-tree
        :data="treeData"
        :props="defaultProps"
        :highlight-current="true"
        :default-expanded-keys="[this.label]"
        node-key="label"
        ref="treeBox"
        class="elTree"
        @node-click="handleNodeClick"
        lazy
        :load="loadNode"
      ></el-tree>
    </div>
</template>
<script>
import { getTdsbbmTree, getTdsbbmGyTree } from "../../../../api/dmgl/sbbm";
export default {
    data() {
        return {
        treeData: [],
        defaultProps: {
            label: "label",
            children: "child",
            isLeaf: "leaf",
        },
        label: ""
        };
    },

    props: {
      propData: {
        type: String,
        default: function() {
          return '';
        }
      }
    },

    methods: {
        /**点击树节点 */
        handleNodeClick(data) {
            console.log(data)
            const that = this
            let tableObj = {}
            if (that.propData == 'zy') {
                tableObj = {
                    tdsbm: data.data.tdsbm,
                    sbfl: '',
                    activeName: that.propData
                }
            } else {
                tableObj = {
                    sbfl: data.data.sbfl,
                    tdsbm: '',
                    activeName: that.propData
                }
            }
            this.$emit("handleClickTreeNodeItem", tableObj);
        },
        /**树的懒加载 */
        loadNode(node, resolve) {
            if (node.level === 0) { 
                this.getTdsbbmTreeOne(resolve);
            } else if (node.level === 1) {
                this.getTdsbbmTreeRoot(node, resolve)
            } else if (node.level > 1) {
                this.getTdsbbmTree(node, resolve)
            }
        },

        /**获取一级节点数据 */
        getTdsbbmTreeOne(resolve){
            let res = []
            if (this.propData == 'zy') {
                res[0] = {
                    id: "0",
                    label: '中压设备编码',
                    icon: null,
                    children: null,
                    data: {
                        id: "",
                        sbqc: "",
                        ssfl: "",
                        tdsbm: "",
                        tdsbmc: "",
                        tjbzm: "",
                    }
                }
            } else {
                res[0] = {
                    id: "0",
                    label: '高压设备编码',
                    icon: null,
                    children: null,
                    data: {
                        id: "0",
                        sbfl: "",
                        sbqc: "",
                        ssfl: "",
                        tdsbm: "",
                        tdsbmc: "",
                        tjbzm: "",
                        xsdm: ""
                    }
                }
            }
            this.label = res[0].label
            this.treeData = res;
            return resolve(res);
        },

        /**获取树 根节点数据*/
        async getTdsbbmTreeRoot(node, resolve){
            let reqData = {}
            let res = null
            if (this.propData == 'zy') {
                reqData = {
                    root: "1",
                    tdsbm: node.data.id === "0"? "" : ""
                }
                res = await getTdsbbmTree(reqData);
            } else {
                reqData = {
                    root: "1",
                    sbfl: node.data.id === "0"? "" : ""
                }
                res = await getTdsbbmGyTree(reqData);
            }          
            if(res.code === 0){
                let req = res.data
                return resolve(res.data);
            }else{
                this.$message.error(res.message)
            }

        },

        /**获取下级节点数据 */
        async getTdsbbmTree(node, resolve) {
            let reqData = {}
            let res = null
            if (this.propData == 'zy') {
                reqData = {
                    root: "0",
                    tdsbm: node.data.data.tdsbm
                }
                res = await getTdsbbmTree(reqData);
            } else {
                reqData = {
                    root: "0",
                    sbfl: node.data.data.sbfl
                }
                res = await getTdsbbmGyTree(reqData);
            }
            if (res.code === 0) {
                let req = res.data;
                return resolve(req);
            } else {
                this.$message.error(res.message);
            }
        }
    },
};
</script>
<style lang='scss' scoped>
label {
  font-weight: 400 !important;
}
.elTree{
  width: 100%;
  height: 100%;
  overflow: auto;
}
/deep/ .el-tree > .el-tree-node {
  min-width: 100%;
  display: inline-block;
}
</style>
